﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Singleton;

namespace vngoodlife.Models
{
    public class ArticleTagModels
    {
        internal bool DeleteTagOfArticle(int TagID, int articleID)
        {
            try
            {
            var tag = DataSingleton.Instance.GetLinQContext().Article_Tags
                    .Single(u => u.ArticleID == articleID && u.TagsID == TagID);
            DataSingleton.Instance.GetLinQContext().Article_Tags.DeleteOnSubmit(tag);
            DataSingleton.Instance.GetLinQContext().Article_Tags.Context.SubmitChanges();
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
                return false;
            }
            return true;
        }

        internal bool InsertTag(Article_Tag artTag)
        {
            try
            {
                if (!DataSingleton.Instance.GetLinQContext().Article_Tags.Any(u => u == artTag))
                {
                    DataSingleton.Instance.GetLinQContext().Article_Tags.InsertOnSubmit(artTag);
                    DataSingleton.Instance.GetLinQContext().Article_Tags.Context.SubmitChanges();
                }
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
                return false;
            }
            return true;
        }

        /// <summary>
        /// insert many tags of one article
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tagIDs"></param>
        /// <returns></returns>
        internal bool InsertManyTag(int id, string tagIDs)
        {
            try
            {
                DataSingleton.Instance.GetLinQContext().sp_TagsInArticleInsert(id, tagIDs);
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
                return false;
            }
            return true;
        }
    }
}